package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.RequireLogin;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.service.IBidRequestService;
import cn.wolfcode.p2p.bussness.service.IExpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
public class BidRequestController {

    @Autowired
    private IBidRequestService bidRequestService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IRealAuthService realAuthService;

    @Autowired
    private IExpAccountService expAccountService;

    @RequestMapping("/borrowInfo")
    @RequireLogin
    public String borrowInfoPage(Model model) {
        UserInfo userInfo = userInfoService.getCurrent();
        //判断是否有借款申请正在审核
        if (userInfo.isHasBidRequestApplyProcess()) {
            //如果有的话 则返回这个视图
            return "borrow_apply_result";
        }
        //account
        model.addAttribute("account", accountService.get(UserContext.getCurrent().getId()));
        //minBidRequestAmount 最小借款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        //minBidAmount 最小投标金额
        model.addAttribute("minBidAmount", Constants.BID_AMOUNT_MIN);
        return "borrow_apply";
    }

    /**
     * 申请按钮
     * @param bidRequest
     * @return
     */
    @RequestMapping("/borrow_apply")
    @ResponseBody
    public AjaxResult borrowApply(BidRequest bidRequest) {
        AjaxResult result = null;
        try {
            bidRequestService.apply(bidRequest);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    @RequestMapping("/borrow_info")
    public String borrowInfo(Long id, Model model) {
        BidRequest bidRequest = bidRequestService.get(id);
        String returnPage = "";
        if (bidRequest != null) {
            //bidRequest
            model.addAttribute("bidRequest", bidRequestService.get(id));

            //如果是信用标 就需要下面的条件
           if(bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL){
               //userInfo
               UserInfo creatUserInfo = userInfoService.get(bidRequest.getCreateUser().getId());
               model.addAttribute("userInfo", creatUserInfo);
               //realAuth
               RealAuth realAuth = realAuthService.get(creatUserInfo.getRealAuthId());
               model.addAttribute("realAuth", realAuth);
               //是否已经登录
               if (UserContext.getCurrent() == null) {
                   model.addAttribute("self", false);
               } else {
                   //判断当前登陆的人是不是借款人
                   if (UserContext.getCurrent().getId().equals(bidRequest.getCreateUser().getId())) {
                       //如果是本人的话
                       model.addAttribute("self", true);
                   } else {
                       model.addAttribute("self", false);
                       model.addAttribute("account", accountService.get(UserContext.getCurrent().getId()));
                   }
               }
               returnPage =  "borrow_info";
           }else if(bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_EXP){
               /*model.addAttribute("self", false);*/
               //如果当前没有 用户 那我就不共享我账户出来
               if(UserContext.getCurrent() != null){
                   model.addAttribute("expAccount", expAccountService.get(UserContext.getCurrent().getId()));
               }
               returnPage = "exp_borrow_info";
           }
        }
        return returnPage;
    }

    @RequestMapping("/borrow_bid")
    @ResponseBody
    public AjaxResult bid(Long bidRequestId, BigDecimal amount) {
        AjaxResult result = null;
        try {
            bidRequestService.bid(bidRequestId,amount);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
